import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { Router, ActivatedRoute, NavigationExtras} from '@angular/router';
import { BusinessCircleService } from '../business-circle.service';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { defalutImg } from '../comm';
import { PermissionService } from '../../../service/permission.service';
import { NzTableQueryParams } from 'ng-zorro-antd/table';

@Component({
  selector: 'app-report-manage',
  templateUrl: './report-manage.component.html',
  styleUrls: ['./report-manage.component.scss']
})
export class ReportManageComponent implements OnInit {

  // seachForm
  seachParams: any = {
    type: '0', // 举报类型
    status: '' // 状态
  };
  sortParams: any = {
    sortField: '',
    sort: '' // 0表示正序 1表示倒序
  };
  reportTypeOptions: any = [
    { label: '帖子举报', value: '0' },
    { label: '评论举报', value: '1' }
  ]
  stateOptions: any = [
    { label: '待处理', value: '0' },
    { label: '下架', value: '1' },
    { label: '无效举报', value: '2' }
  ];

  // table
  tableForms: any = {
    tableLoading: false,
    page: 1,
    pageSize: 20,
    total: 0,
    pages: 0
  };
  // 表格data
  tableData: any = [];
  activeTableType: any = '0';

  static updateCache: any = null;

  constructor(
    private fb: FormBuilder,
    private message: NzMessageService,
    private router: Router,
    private services: BusinessCircleService,
    public permission: PermissionService,
  ) { }

  ngOnInit(): void {
    ReportManageComponent.updateCache = () => {
      this.loadTable();
    }
    this.loadTable();
  }

  async loadTable() {
    try {
      let params: any = {
        ...this.seachParams,
        ...this.sortParams,
        page: this.tableForms.page,
        pageSize: this.tableForms.pageSize
      }
      for (let key in params) {
        if (params[key] == '' || params[key] == undefined || params[key] == null) {
          delete params[key];
        }
      }
      // return
      this.tableForms.tableLoading = true;
      const res: any = await this.services.queryReportManageTableData(params);
      this.tableForms.tableLoading = false;
      this.activeTableType = this.seachParams.type;

      if (res.code == 0) {
        const { records, total }: any = res.data;
        this.tableData = records || [];
        this.tableForms.total = total || 0;
      }
    } catch (error) {
      this.tableForms.tableLoading = false;
      this.tableData = [];
      this.tableForms.total = 0;
      this.activeTableType = '0';
    }
  }

  // 查询
  queryData() {
    this.tableForms.page = 1;
    this.loadTable();
  }
  // 重置
  resetData() {
    this.seachParams = {
      type: '0', // 举报类型
      status: '' // 状态
    }
    this.tableForms.page = 1;
    this.loadTable();
  }

  // 举报类型change
  statusChange(ev: any) {
    let delIndex: any = null;
    if (ev == 0) {
      delIndex = this.stateOptions.findIndex((item: any) => { return item.value == '3' });
      this.stateOptions.push({ label: '下架', value: '1' });
    } else {
      delIndex = this.stateOptions.findIndex((item: any) => { return item.value == '1' });
      this.stateOptions.push({ label: '删除', value: '3' });
    }
    this.stateOptions.splice(delIndex, 1);
  }

  // 排序
  onQueryParamsChange(params: NzTableQueryParams) {
    const { pageSize, pageIndex, sort, filter } = params;
    const currentSort = sort.find(item => item.value !== null);
    const sortField = (currentSort && currentSort.key) || null;
    const sortOrder = (currentSort && currentSort.value) || null;
    // 0表示正序 1表示倒序
    if (sortOrder) {
      this.sortParams.sortField = sortField;
      this.sortParams.sort = sortOrder == 'ascend' ? '0' : '1';
    } else {
      this.sortParams.sortField = '';
      this.sortParams.sort = '';
    }
    this.loadTable();
  }

  /**
   * table 操作
   * @param item 
   * @param num 0：下架帖子，1：删除评论，2：无效举报
   */
  modifyData(item: any, num: number) {
    switch (num) {
      case 0:
        console.log('下架帖子');
        this.operationStatusTable(item, 1);
        break;
      case 1:
        console.log('删除评论');
        this.operationStatusTable(item, 3);
        break;
      case 2:
        console.log('无效举报');
        this.operationStatusTable(item, 2);
        break;
    }
  }
  // 删除 | 下架 | 无效举报 interface
  async operationStatusTable(item: any, status: any) {
    try {
      // 状态：0-待处理、1-下架、2-无效举报 3 删除
      const res: any = await this.services.updateReportItemData({id: item.id, status: status, type: this.seachParams.type});
      if (res.code == 0) {
        this.message.success('操作成功');
        this.loadTable();
      } else {
        this.message.error(res.message || '操作失败');
      }
    } catch (error: any) {
      this.message.error(error);
    }
  }

  // 当前页码改变时的回调函数
  onPageIndexChange(index: number) {
    this.tableForms.page = index;
    this.loadTable();
  }
  // 页数改变时回调
  onPageSizeChange(sizeNum: number) {
    this.tableForms.page = 1;
    this.tableForms.pageSize = sizeNum;

    this.loadTable();
  }
}
